#!/usr/bin/env node
const log=console.log,
    {con}=require('./common.js');

async function getEssayByName(name){
    const sql=`select id,title,publish from essay where title like '%${name}%';`;
    let [rows]=await con.execute(sql);
    return rows
}

async function getEssayByUser(userid){
    const sql=`select essay.id,essay.title,essayimage.img from essay,essayimage where user=${userid} and essay.id=essayimage.id group by essay.id;`;
    let [rows]=await con.execute(sql);
    return rows
}

async function getEssayUpload(id,title,content,date){
    const sqlCount='select count(id) count from essay';
    let [rowsCount]=await con.execute(sqlCount);
    let count=rowsCount[0].count
    const sql= `insert into essay (id,user,title,content,publish) values(${count},${id},'${title}','${content}','${date}');`
    let [rows]=await con.execute(sql);

    return count;
}

async function getEssayPics(id,path){
        let sqlimg=`insert into essayimage (id,img) values(${id},'${path}');`;
        let [rowimg]=await con.execute(sqlimg)
        log(rowimg)
        
    
}


module.exports={getEssayByName,getEssayByUser,getEssayUpload,getEssayPics}